/*
 * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.mhb.generator.config.po;

import com.mhb.generator.config.interfaces.DbQueryInterface;
import com.mhb.generator.config.rules.DbColumnType;
import com.mhb.generator.config.rules.DbType;
import com.mhb.generator.config.rules.IColumnType;
import com.mhb.generator.util.StringUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
 * 表字段信息
 *
 * @author YangHu
 * @since 2016-12-03
 */
@Data
@Accessors(chain = true)
public class TableField {

    // 是否是主键
    private boolean keyFlag = false;
    // 主键是否为自增类型
    private boolean keyIdentityFlag = false;
    // 是否允许为null
    private boolean notNull = false;
    // 数据库字段属性
    private String type;
    // 属性名称
    private String propertyName;
    // 字段类型
    private IColumnType columnType;
    // 数据库列
    private String column;
    // 字段说明
    private String comment;
    // 屬性类型
    private String propertyType;

    /**
     * 自定义查询字段列表
     */
    private Map<String, Object> customMap;

    public TableField() {
    }

    public TableField(boolean keyFlag, boolean keyIdentityFlag, String type, String propertyName, IColumnType columnType, String column, String comment) {
        this.keyFlag = keyFlag;
        this.keyIdentityFlag = keyIdentityFlag;
        this.type = type;
        this.propertyName = propertyName;
        this.columnType = columnType;
        this.column = column;
        this.comment = comment;
    }

    /**
     * 按JavaBean规则来生成get和set方法
     */
    public String getCapitalName() {
        if (propertyName.length() <= 1) {
            return propertyName.toUpperCase();
        }
        String setGetName = propertyName;
        // 判断是否是boolean
        if (DbColumnType.BASE_BOOLEAN.getType().equalsIgnoreCase(columnType.getType())) {
            setGetName = StringUtil.removeIsPrefixIfBoolean(setGetName, Boolean.class);
        }
        // 第一个字母 小写、 第二个字母 大写 ，特殊处理
        String firstChar = setGetName.substring(0, 1);
        if (Character.isLowerCase(firstChar.toCharArray()[0])
                && Character.isUpperCase(setGetName.substring(1, 2).toCharArray()[0])) {
            return firstChar.toLowerCase() + setGetName.substring(1);
        }
        return firstChar.toUpperCase() + setGetName.substring(1);
    }

    /**
     * 根据传入的结果集来对主键列赋值
     *
     * @param rs     结果集
     * @param dbType db查询接口
     * @throws SQLException
     */
    public boolean getPriAndExtra(ResultSet rs, DbQueryInterface dbType,TableInfo tableInfo) throws SQLException {
        String key = rs.getString(dbType.fieldKey());
        // 判断是不是主键
        if (StringUtil.isNotNull(key) && key.toUpperCase().equalsIgnoreCase("PRI")) {
            this.setKeyFlag(true);
            String extra = rs.getString(dbType.fieldExtra());
            // 判断是不是自增
            if (StringUtil.isNotNull(extra) && extra.toLowerCase().equalsIgnoreCase("auto_increment")) {
                this.setKeyIdentityFlag(true);
            }
            return true;
        }
        return false;
    }

    public String getXmlEntryParameters(){
        return StringUtil.format("#{%s}",propertyName);
    }
    public String getXmlListEntryParameters(){
        return StringUtil.format("#{item.%s}",propertyName);
    }

    public String getXmlUpdateEntryParameters(){
        return StringUtil.format("%s=#{%s}",column,propertyName);
    }

}
